package com.memory.monitor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.memory.monitor.annotation.AuthCheck;
import com.memory.monitor.common.BaseResponse;
import com.memory.monitor.common.DeleteRequest;
import com.memory.monitor.common.ErrorCode;
import com.memory.monitor.common.ResultUtils;
import com.memory.monitor.constant.UserConstant;
import com.memory.monitor.exception.BusinessException;
import com.memory.monitor.exception.ThrowUtils;
import com.memory.monitor.model.dto.monitor.MonitorQuerryRequest;
import com.memory.monitor.model.entity.Monitor;
import com.memory.monitor.model.vo.MonitorVO;
import com.memory.monitor.service.MonitorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/monitor")
@Slf4j
public class MonitorController {
    @Resource
    private MonitorService monitorService;

    /**
     * 分页获取用户列表（仅管理员）
     *
     * @param monitorQuerryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<Monitor>> listMonitorByPage(@RequestBody MonitorQuerryRequest monitorQuerryRequest,
                                                      HttpServletRequest request) {
        long current = monitorQuerryRequest.getCurrent();
        long size = monitorQuerryRequest.getPageSize();
        Page<Monitor> userPage = monitorService.page(new Page<>(current, size));

        return ResultUtils.success(userPage);

    }

    /**
     * 创建用户
     *
     * @param monitorVO
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addMonitor(@RequestBody MonitorVO monitorVO, HttpServletRequest request) {
        if (monitorVO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Monitor monitor = new Monitor();
        BeanUtils.copyProperties(monitorVO, monitor);
        boolean result = monitorService.save(monitor);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(monitor.getId());
    }

    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteMonitor(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = monitorService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户
     *
     * @param monitorVO
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateMonitor(@RequestBody MonitorVO monitorVO,
                                            HttpServletRequest request) {
        if (monitorVO == null || monitorVO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Monitor monitor = new Monitor();
        BeanUtils.copyProperties(monitorVO, monitor);
        boolean result = monitorService.updateById(monitor);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }
}